# -*- coding: utf-8 -*-

from PyQt4.QtCore import QCoreApplication, QEventLoop
import getopt, os, sys, shlex
from operator import itemgetter
from subprocess import *
import sqlite3 as sqlite
import xml.dom.minidom
from xml.dom.minidom import Node

dbName = "commits.db"

if os.path.exists(dbName):
    connection = sqlite.connect(dbName)
    cursor = connection.cursor()

else:
    print "No db file is found..."
    sys.exit(1)
    
defaultDirectory = ""

def getSubversionRepo(serverUrl, projectDirectory, browser):    
    browser.clear()

    global defaultDirectory
    defaultDirectory = os.getcwd()

    if os.path.exists(projectDirectory):
        browser.append("Destination already exists!... Will UPDATE:\n\n%s" % serverUrl + "\n\nHold on please...\n\n\n")
        QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
        
        os.chdir(projectDirectory)
        proc = Popen("svn cleanup", shell=True, stdout=PIPE)
        while True:
            out = proc.stdout.readline()
            if out == '' and proc.poll() != None:
                break
            if out != '':
                out = out.replace('\n', '')
                browser.append(out)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
        
	proc = Popen("svn update", shell=True, stdout=PIPE)
        while True:
            out = proc.stdout.readline()
            if out == '' and proc.poll() != None:
                break
            if out != '':
                out = out.replace('\n', '')
                browser.append(out)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

    else:
        os.system("mkdir " + projectDirectory)
        os.chdir(projectDirectory)
	browser.append("Starting to CHECKOUT:\n\n%s" % serverUrl + "\n\nHold on please...\n\n\n")
        QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
        
        proc = Popen("svn checkout " + serverUrl + " .", shell=True, stdout=PIPE)
        while True:
            out = proc.stdout.readline()
            if out == '' and proc.poll() != None:
                break
            if out != '':
                out = out.replace('\n', '')
                browser.append(out)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

    os.chdir(defaultDirectory)

def parseSubversionLog(projectName, projectVersion, projectRootUrl, projectDirectory, browser):

    createSubversionRepoStatistics(projectDirectory, browser)

    global defaultDirectory
    defaultDirectory = os.getcwd()
    
    os.chdir(projectDirectory)
    
    #"********** REPO STATISTICS **********"
 
    doc = xml.dom.minidom.parse("repo-statistics.xml")

    for node in doc.getElementsByTagName("RepoStatistics"):
        projectPeriod = node.getAttribute("period")
        projectNumberOfFiles = node.getAttribute("numberfiles")
        projectTotalLoc = node.getAttribute("totalloc")

        proTimeLine = projectPeriod.split("to")
    
        projectStartDate = proTimeLine[0]
        projectStartDate = projectStartDate.strip() + " 00:00:00"
    
        projectEndDate = proTimeLine[1]
        projectEndDate = projectEndDate.strip() + " 00:00:00"
    
        cursor.execute('INSERT INTO projectTable VALUES (null, ?, ?, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), projectStartDate.strip(), projectEndDate.strip(), projectNumberOfFiles.strip(), projectTotalLoc.strip()))

    #"********** COMMITTERS **********"

    for node in doc.getElementsByTagName("Author"):
        subNode = node.getElementsByTagName("AuthorId")
        for node2 in subNode:
            authorName = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.TEXT_NODE:
                    authorName = node3.data

        subNode = node.getElementsByTagName("Changes")
        for node2 in subNode:
            authorChanges = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.TEXT_NODE:
                    authorChanges = node3.data

        commitsDone = authorChanges.split(" ")
        authorChanges = commitsDone[0]
        cmmtsPercentage = commitsDone[1]
        commitsPercentage = cmmtsPercentage.split("%")
        tempPercentage =  commitsPercentage[0]
        temp2Percentage = tempPercentage.split("(")
        authorPercentage = temp2Percentage[1]

        subNode = node.getElementsByTagName("LinesOfCode")
        for node2 in subNode:
            authorLinesOfCode = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.TEXT_NODE:
                    authorLinesOfCode = node3.data

        authorLnsChanged = authorLinesOfCode.split(" ")
        authorLinesOfCode = authorLnsChanged[0]
        authorLinesOfCodePer = authorLnsChanged[1]
        authorLnsChngd = authorLinesOfCodePer.split("%")
        tempauthorLinesOfCode = authorLnsChngd[0]
        temp2authorLinesOfCode = tempauthorLinesOfCode.split("(")
        authorLinesOfCodePercentage = temp2authorLinesOfCode[1]
  
        subNode = node.getElementsByTagName("LinesPerChange")
        for node2 in subNode:
            authorLinesPerChange = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.TEXT_NODE:
                    authorLinesPerChange = node3.data          

        cursor.execute('INSERT INTO authorTable VALUES (null, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), authorName.strip(), authorLinesOfCode.strip()))

    #"********** COMMITS **********"

    for node in doc.getElementsByTagName("Commit"):
        revisionNo = node.getAttribute("revision")
        authorName = node.getAttribute("author")
        commitDt = node.getAttribute("date")
        locChange = node.getAttribute("loc_change")    

        subNode = node.getElementsByTagName("Comment")
        for node2 in subNode:
            commitMessage = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.CDATA_SECTION_NODE:
                    commitMessage = node3.data

        commitDate = commitDt + ":00"
    
        cursor.execute('INSERT INTO commitTable VALUES (null, ?, ?, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), revisionNo.strip(), authorName.strip(), commitDate.strip(), commitMessage.strip()))
        
        for node2 in node.getElementsByTagName("File"):
            fileAction = node2.getAttribute("action")       
        
            subNode = node2.getElementsByTagName("Path")
            for node3 in subNode:
                filePath = ""
            for node4 in node3.childNodes:
                if node4.nodeType == Node.TEXT_NODE:
                    filePath = node4.data
                
            subNode = node2.getElementsByTagName("LocAdd")
            for node3 in subNode:
                fileLocAdd = ""
                for node4 in node3.childNodes:
                    if node4.nodeType == Node.TEXT_NODE:
                        fileLocAdd = node4.data

                if fileAction:
                    cursor.execute('INSERT INTO fileTable VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), revisionNo.strip(), commitDate.strip(), authorName.strip(), filePath.strip(), fileLocAdd.strip(), fileLocAdd.strip()))

                elif not fileAction:
                    cursor.execute('INSERT INTO fileTable VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), revisionNo.strip(), commitDate.strip(), authorName.strip(), filePath.strip(), fileLocAdd.strip(), fileLocAdd.strip()))
        
    connection.commit()
    
    os.chdir(defaultDirectory)

def createSubversionRepoStatistics(projectDirectory, browser):
    browser.clear()

    global defaultDirectory
    defaultDirectory = os.getcwd()
    
    os.chdir(projectDirectory)
    
    browser.append("Start UPDATING Svn Repo...\n")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    
    proc = Popen("svn update .", shell=True, stdout=PIPE)
    while True:
        out = proc.stdout.readline()
        if out == '' and proc.poll() != None:
            break
        if out != '':
            out = out.replace('\n', '')
            browser.append(out)
            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                
    browser.append("Svn Repo is UPDATED.\n")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    

    browser.append("\nStart EXTRACTING Svn Repo Commit Logs...")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    
    proc = Popen("svn log -v --xml . > svn.log", shell=True, stdout=PIPE)
    while True:
        out = proc.stdout.readline()
        if out == '' and proc.poll() != None:
            break
        if out != '':
            out = out.replace('\n', '')
            browser.append(out)
            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                
    browser.append("Svn Repo Commit Logs is EXTRACTED.\n")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    

    browser.append("\nStart CREATING Repo-Statistics.xml...")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    
    proc = Popen("java -jar ../statsvn.jar -include **/*.c:**/*.cpp:**/*.java:**/*.h -xml svn.log .", shell=True, stdout=PIPE)
    while True:
        out = proc.stdout.readline()
        if out == '' and proc.poll() != None:
            break
        if out != '':
            out = out.replace('\n', '')
            browser.append(out)
            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                
    browser.append("Repo-Statistics.xml is CREATED.\n")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

    os.chdir(defaultDirectory)
        
def getCvsRepo(serverUrl, projectDirectory, browser):
    browser.clear()

    global defaultDirectory
    defaultDirectory = os.getcwd()

    if os.path.exists(projectDirectory):
        browser.append("Destination already exists!... Will UPDATE:\n\n%s" % serverUrl + "\n\nHold on please...\n\n\n")
        QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
        
        os.chdir(projectDirectory)
        proc = Popen("cvs -d:pserver:anonymous@%s login" % serverUrl, shell=True, stdout=PIPE)
        while True:
            out = proc.stdout.readline()
            if out == '' and proc.poll() != None:
                break
            if out != '':
                out = out.replace('\n', '')
                browser.append(out)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                
        proc = Popen("cvs update -r .", shell=True, stdout=PIPE)
        while True:
            out = proc.stdout.readline()
            if out == '' and proc.poll() != None:
                break
            if out != '':
                out = out.replace('\n', '')
                browser.append(out)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

    else:
        os.system("mkdir " + projectDirectory)
        os.chdir(projectDirectory)
	browser.append("Starting to CHECKOUT:\n\n%s" % serverUrl + "\n\nHold on please...\n\n\n")
        QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
        
        proc = Popen("cvs -d:pserver:anonymous@%s login" % serverUrl, shell=True, stdout=PIPE)
        while True:
            out = proc.stdout.readline()
            if out == '' and proc.poll() != None:
                break
            if out != '':
                out = out.replace('\n', '')
                browser.append(out)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
       
	proc = Popen("cvs -z3 -d:pserver:%s co -P ." % serverUrl, shell=True, stdout=PIPE)
        while True:
            out = proc.stdout.readline()
            if out == '' and proc.poll() != None:
                break
            if out != '':
                out = out.replace('\n', '')
                browser.append(out)
                QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

    os.chdir(defaultDirectory)

def parseCvsLog(projectName, projectVersion, projectRootUrl, projectDirectory, browser):
    
    createCvsRepoStatistics(projectRootUrl, projectDirectory, browser)

    global defaultDirectory
    defaultDirectory = os.getcwd()
    
    os.chdir(projectDirectory)
    
    #"********** REPO STATISTICS **********"
 
    doc = xml.dom.minidom.parse("repo-statistics.xml")

    for node in doc.getElementsByTagName("RepoStatistics"):
        projectPeriod = node.getAttribute("period")
        projectNumberOfFiles = node.getAttribute("numberfiles")
        projectTotalLoc = node.getAttribute("totalloc")

        proTimeLine = projectPeriod.split("to")
    
        projectStartDate = proTimeLine[0]
        projectStartDate = projectStartDate.strip() + " 00:00:00"
    
        projectEndDate = proTimeLine[1]
        projectEndDate = projectEndDate.strip() + " 00:00:00"
    
        cursor.execute('INSERT INTO projectTable VALUES (null, ?, ?, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), projectStartDate.strip(), projectEndDate.strip(), projectNumberOfFiles.strip(), projectTotalLoc.strip()))

    #"********** COMMITTERS **********"

    for node in doc.getElementsByTagName("Author"):
        subNode = node.getElementsByTagName("AuthorId")
        for node2 in subNode:
            authorName = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.TEXT_NODE:
                    authorName = node3.data

        subNode = node.getElementsByTagName("Changes")
        for node2 in subNode:
            authorChanges = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.TEXT_NODE:
                    authorChanges = node3.data

        commitsDone = authorChanges.split(" ")
        authorChanges = commitsDone[0]
        cmmtsPercentage = commitsDone[1]
        commitsPercentage = cmmtsPercentage.split("%")
        tempPercentage =  commitsPercentage[0]
        temp2Percentage = tempPercentage.split("(")
        authorPercentage = temp2Percentage[1]

        subNode = node.getElementsByTagName("LinesOfCode")
        for node2 in subNode:
            authorLinesOfCode = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.TEXT_NODE:
                    authorLinesOfCode = node3.data

        authorLnsChanged = authorLinesOfCode.split(" ")
        authorLinesOfCode = authorLnsChanged[0]
        authorLinesOfCodePer = authorLnsChanged[1]
        authorLnsChngd = authorLinesOfCodePer.split("%")
        tempauthorLinesOfCode = authorLnsChngd[0]
        temp2authorLinesOfCode = tempauthorLinesOfCode.split("(")
        authorLinesOfCodePercentage = temp2authorLinesOfCode[1]
  
        subNode = node.getElementsByTagName("LinesPerChange")
        for node2 in subNode:
            authorLinesPerChange = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.TEXT_NODE:
                    authorLinesPerChange = node3.data          

        cursor.execute('INSERT INTO authorTable VALUES (null, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), authorName.strip(), authorLinesOfCode.strip()))

    #"********** COMMITS **********"

    for node in doc.getElementsByTagName("Commit"):
        revisionNo = node.getAttribute("revision")
        authorName = node.getAttribute("author")
        commitDt = node.getAttribute("date")
        locChange = node.getAttribute("loc_change")    

        subNode = node.getElementsByTagName("Comment")
        for node2 in subNode:
            commitMessage = ""
            for node3 in node2.childNodes:
                if node3.nodeType == Node.CDATA_SECTION_NODE:
                    commitMessage = node3.data

        commitDate = commitDt + ":00"
    
        cursor.execute('INSERT INTO commitTable VALUES (null, ?, ?, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), revisionNo.strip(), authorName.strip(), commitDate.strip(), commitMessage.strip()))
        
        for node2 in node.getElementsByTagName("File"):
            fileAction = node2.getAttribute("action")       
        
            subNode = node2.getElementsByTagName("Path")
            for node3 in subNode:
                filePath = ""
            for node4 in node3.childNodes:
                if node4.nodeType == Node.TEXT_NODE:
                    filePath = node4.data
                
            subNode = node2.getElementsByTagName("LocAdd")
            for node3 in subNode:
                fileLocAdd = ""
                for node4 in node3.childNodes:
                    if node4.nodeType == Node.TEXT_NODE:
                        fileLocAdd = node4.data

                if fileAction:
                    cursor.execute('INSERT INTO fileTable VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), revisionNo.strip(), commitDate.strip(), authorName.strip(), filePath.strip(), fileLocAdd.strip(), fileLocAdd.strip()))

                elif not fileAction:
                    cursor.execute('INSERT INTO fileTable VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?)',(projectName.strip(), projectVersion.strip(), revisionNo.strip(), commitDate.strip(), authorName.strip(), filePath.strip(), fileLocAdd.strip(), fileLocAdd.strip()))
        
    connection.commit()
    
    os.chdir(defaultDirectory)    

def createCvsRepoStatistics(serverUrl, projectDirectory, browser):
    browser.clear()

    global defaultDirectory
    defaultDirectory = os.getcwd()
    
    os.chdir(projectDirectory)
    
    browser.append("Start UPDATING Cvs Repo...\n")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    
    proc = Popen("cvs -d:pserver:anonymous@%s login" % serverUrl, shell=True, stdout=PIPE)
    while True:
        out = proc.stdout.readline()
        if out == '' and proc.poll() != None:
            break
        if out != '':
            out = out.replace('\n', '')
            browser.append(out)
            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                
    proc = Popen("cvs update -r .", shell=True, stdout=PIPE)
    while True:
        out = proc.stdout.readline()
        if out == '' and proc.poll() != None:
            break
        if out != '':
            out = out.replace('\n', '')
            browser.append(out)
            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                
    browser.append("Cvs Repo is UPDATED.\n")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    

    browser.append("\nStart EXTRACTING Cvs Repo Commit Logs...")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    
    proc = Popen("cvs log . > cvs.log", shell=True, stdout=PIPE)
    while True:
        out = proc.stdout.readline()
        if out == '' and proc.poll() != None:
            break
        if out != '':
            out = out.replace('\n', '')
            browser.append(out)
            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                
    browser.append("Cvs Repo Commit Logs is EXTRACTED.\n")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    

    browser.append("\nStart CREATING Repo-Statistics.xml...")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
    
    proc = Popen("java -jar ../statcvs.jar -include **/*.c:**/*.cpp:**/*.java:**/*.h -xml cvs.log .", shell=True, stdout=PIPE)
    while True:
        out = proc.stdout.readline()
        if out == '' and proc.poll() != None:
            break
        if out != '':
            out = out.replace('\n', '')
            browser.append(out)
            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
                
    browser.append("Repo-Statistics.xml is CREATED.\n")
    QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

    os.chdir(defaultDirectory)
